package com.his.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.admin.service.RoleMenuService;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.SelectRoleMenuDto;
import com.his.dao.domain.entity.RoleMenu;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
@Api(value = "角色菜单模块")
public class RoleMenuController {
    @Resource
    private RoleMenuService roleMenuService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取角色菜单列表",notes = "获取角色菜单列表")
    public RestResult selectRoleMenu(@RequestBody SelectRoleMenuDto selectRoleMenuDto) {
        return roleMenuService.all(selectRoleMenuDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加角色菜单",notes = "增加角色菜单")
    public RestResult add(@RequestBody RoleMenu roleMenu) {
       if(roleMenuService.save(roleMenu)) {
           int id = roleMenuService
                   .getOne(new QueryWrapper<RoleMenu>()
                           .orderByDesc("menu_role_id")
                           .last("limit 1"))
                   .getMenuRoleId();
           return new RestResultBuilder().success(id);
       }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除角色菜单",notes = "删除角色菜单")
    public RestResult del(@RequestBody RoleMenu roleMenu) {
        if(roleMenuService.removeById(roleMenu)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }
}